package com.huaxinkaiye.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.huaxinkaiye.R;
import com.huaxinkaiye.application.HuaXinApplication;
import com.huaxinkaiye.bean.PictureInfo;
import com.huaxinkaiye.bean.RowsBean;
import com.huaxinkaiye.lookbigphoto.PhotosDialog;
import com.huaxinkaiye.utils.FileImageUpload;
import com.huaxinkaiye.utils.ImageUtils;
import com.huaxinkaiye.utils.OptionsUtil;
import com.huaxinkaiye.utils.PhotoImplActivity;
import com.huaxinkaiye.utils.ProgressUtil;
import com.huaxinkaiye.utils.SimpleClient;
import com.huaxinkaiye.utils.ToastUtil;
import com.huaxinkaiye.utils.UrlUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CarChaiJieActivity extends PhotoImplActivity implements
		OnClickListener, OnLongClickListener {
	private ImageView im_back, im_cancle, im_queren, im_che1, im_che2, im_che3;
	private TextView te_chepaihao, te_chuangjiantime, te_danganbianhao;
	private RowsBean rowsBean;
	private int zhaopianid;
	private int removeid;
	public TextView te_shanchu3, te_shanchu2;
	public NetworkImageView im_che;
	public List<NameValuePair> params;
	public ProgressUtil progress;
	public String picUrl = "";
	public ArrayList<String> picPathA;
	public ArrayList<Integer> picPathid;
	public String url1 = "";
	public String url2 = "";
	public String url3 = "";
	public int p2, p3;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				progress.dismiss();
				jiexiRemove(msg.obj.toString());
				break;
			case 3:
				progress.dismiss();
				jiexiPictureData(msg.obj.toString());
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_carchaijie);
		progress = new ProgressUtil(this, "正在上传");
		initView();
	}

	protected void jiexiRemove(String s) {
		// TODO Auto-generated method stub
		Log.e("removepicture", s);
		try {
			JSONObject json = new JSONObject(s);
			if (json.getBoolean("isSuccess")) {
				ToastUtil.show(this, json.getString("message"));
				if (removeid == 2) {
					im_che2.setImageResource(R.drawable.shangchuan);
					url2 = "";
				}
				if (removeid == 3) {
					im_che3.setImageResource(R.drawable.shangchuan);
					url3 = "";
				}
			} else {
				ToastUtil.show(this, json.getString("message"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		im_back = (ImageView) findViewById(R.id.im_back);
		im_back.setOnClickListener(this);
		im_cancle = (ImageView) findViewById(R.id.im_cancle);
		im_cancle.setOnClickListener(this);
		im_queren = (ImageView) findViewById(R.id.im_queren);
		im_queren.setOnClickListener(this);
		im_che = (NetworkImageView) findViewById(R.id.im_che);
		im_che.setOnClickListener(this);
		im_che1 = (ImageView) findViewById(R.id.im_che1);
		im_che1.setOnClickListener(this);
		im_che1.setOnLongClickListener(this);
		im_che2 = (ImageView) findViewById(R.id.im_che2);
		im_che2.setOnLongClickListener(this);
		im_che2.setOnClickListener(this);
		im_che3 = (ImageView) findViewById(R.id.im_che3);
		im_che3.setOnClickListener(this);
		im_che3.setOnLongClickListener(this);
		te_shanchu2 = (TextView) findViewById(R.id.te_shanchu2);
		te_shanchu2.setOnClickListener(this);
		te_shanchu3 = (TextView) findViewById(R.id.te_shanchu3);
		te_shanchu3.setOnClickListener(this);
		te_chepaihao = (TextView) findViewById(R.id.te_chepaihao);
		te_chuangjiantime = (TextView) findViewById(R.id.te_chuangjiantime);
		te_danganbianhao = (TextView) findViewById(R.id.te_danganbianhao);
		rowsBean = (RowsBean) getIntent().getSerializableExtra("rowsBeanInfo");
		for (int i = 0; i < rowsBean.getPictures().size(); i++) {
			if (rowsBean.getPictures().get(i).getFileType() == 1) {
				picUrl = UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath();
				im_che.setImageUrl(picUrl, HuaXinApplication.mImageLoader);
			}
		}
		te_chepaihao.setText("车牌号： " + rowsBean.getPlateNumber());
		te_chuangjiantime.setTag("创建时间： " + rowsBean.getCreateTime());
		te_danganbianhao.setText("档案编号： " + rowsBean.getBarCode());
		picPathA = new ArrayList<String>();
		picPathid = new ArrayList<Integer>();
		for (int i = 0; i < rowsBean.getPictures().size(); i++) {
			if (rowsBean.getPictures().get(i).getFileType() == 2) {
				picPathA.add(UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath());
				picPathid.add(rowsBean.getPictures().get(i).getId());
			}
		}
		if (picPathA.size() == 1) {
			ImageLoader.getInstance().displayImage(picPathA.get(0), im_che1,
					OptionsUtil.optionsAvatar);
			url1 = picPathA.get(0);

		}
		if (picPathA.size() == 2) {
			url1 = picPathA.get(0);
			url2 = picPathA.get(1);
			p2 = picPathid.get(1);
			ImageLoader.getInstance().displayImage(picPathA.get(0), im_che1,
					OptionsUtil.optionsAvatar);
			ImageLoader.getInstance().displayImage(picPathA.get(1), im_che2,
					OptionsUtil.optionsAvatar);
		}
		if (picPathA.size() >= 3) {
			url1 = picPathA.get(0);
			url2 = picPathA.get(1);
			url3 = picPathA.get(2);
			p2 = picPathid.get(1);
			p3 = picPathid.get(2);
			ImageLoader.getInstance().displayImage(picPathA.get(0), im_che1,
					OptionsUtil.optionsAvatar);
			ImageLoader.getInstance().displayImage(picPathA.get(1), im_che2,
					OptionsUtil.optionsAvatar);
			ImageLoader.getInstance().displayImage(picPathA.get(2), im_che3,
					OptionsUtil.optionsAvatar);
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	protected void jiexiPictureData(String string) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		PictureInfo pinfo = gson.fromJson(string, PictureInfo.class);
		if (pinfo.getIsSuccess().equals("true")) {
			ToastUtil.show(this, pinfo.getMessage());
			if (zhaopianid == 1) {
				url1 = UrlUtils.host + pinfo.getPictureInfo().get(0).getPath();
				ImageLoader.getInstance().displayImage(url1, im_che1,
						OptionsUtil.optionsAvatar);
			}
			if (zhaopianid == 2) {
				url2 = UrlUtils.host + pinfo.getPictureInfo().get(0).getPath();
				ImageLoader.getInstance().displayImage(url2, im_che2,
						OptionsUtil.optionsAvatar);
				p2 = pinfo.getPictureInfo().get(0).getId();
			}
			if (zhaopianid == 3) {
				url3 = UrlUtils.host + pinfo.getPictureInfo().get(0).getPath();
				ImageLoader.getInstance().displayImage(url3, im_che3,
						OptionsUtil.optionsAvatar);
				p3 = pinfo.getPictureInfo().get(0).getId();
			}
		} else {
			ToastUtil.show(this, pinfo.getMessage());
		}

		// picPathA = new ArrayList<String>();
		// picPathid = new ArrayList<Integer>();
		// for (int i = 0; i < pinfo.getPictureInfo().size(); i++) {
		// if (pinfo.getPictureInfo().get(i).getFileType() == 2) {
		// picPathA.add(UrlUtils.host
		// + pinfo.getPictureInfo().get(i).getPath());
		// picPathid.add(rowsBean.getPictures().get(i).getId());
		// }
		// }
		// if (picPathA.size() == 1) {
		// ImageLoader.getInstance().displayImage(picPathA.get(0), im_che1,
		// OptionsUtil.optionsAvatar);
		// url1 = picPathA.get(0);
		// }
		// if (picPathA.size() == 2) {
		// url1 = picPathA.get(0);
		// url2 = picPathA.get(1);
		// ImageLoader.getInstance().displayImage(picPathA.get(0), im_che1,
		// OptionsUtil.optionsAvatar);
		// ImageLoader.getInstance().displayImage(picPathA.get(1), im_che2,
		// OptionsUtil.optionsAvatar);
		// }
		// if (picPathA.size() == 3) {
		// url1 = picPathA.get(0);
		// url2 = picPathA.get(1);
		// url3 = picPathA.get(2);
		// ImageLoader.getInstance().displayImage(picPathA.get(0), im_che1,
		// OptionsUtil.optionsAvatar);
		// ImageLoader.getInstance().displayImage(picPathA.get(1), im_che2,
		// OptionsUtil.optionsAvatar);
		// ImageLoader.getInstance().displayImage(picPathA.get(2), im_che3,
		// OptionsUtil.optionsAvatar);
		// }

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_back:
			Intent intent=new Intent(CarChaiJieActivity.this,ChaiJieListActivity.class);
			setResult(RESULT_OK,intent);
			finish();
			break;
		case R.id.im_queren:
			Intent intent2=new Intent(CarChaiJieActivity.this,ChaiJieListActivity.class);
			setResult(RESULT_OK,intent2);
			finish();
		case R.id.im_cancle:
			Intent intent3=new Intent(CarChaiJieActivity.this,ChaiJieListActivity.class);
			setResult(RESULT_OK,intent3);
			finish();
			break;
		case R.id.te_shanchu2:
			if (url2.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			removeid = 2;
			removePicture(p2);
			break;
		case R.id.te_shanchu3:
			if (url3.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			removeid = 3;
			removePicture(p3);
			break;
		case R.id.im_che:
			// 查看大图
			if (picUrl.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(picUrl);
			break;
		case R.id.im_che1:
			// 查看大图
			if (url1.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(url1);
			break;
		case R.id.im_che2:
			// 查看大图
			if (url2.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(url2);
			break;
		case R.id.im_che3:
			// 查看大图
			if (url3.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(url3);
			break;
		}
	}

	private void removePicture(int id) {
		// TODO Auto-generated method stub
		if (HuaXinApplication.isNetWorkConnected()) {
			params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", id + ""));
			progress.show();
			new Thread() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					try {
						String data = SimpleClient.doPost(
								UrlUtils.removepicture_url, params);
						Message mes = new Message();
						mes.what = 1;
						mes.obj = data;
						handler.sendMessage(mes);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void doAfterZoom(Bitmap bitmap) {
		// TODO Auto-generated method stub
		
		if (zhaopianid == 1) {
			im_che1.setImageBitmap(bitmap);
		} else if (zhaopianid == 2) {
			im_che2.setImageBitmap(bitmap);
		} else {
			im_che3.setImageBitmap(bitmap);
		}
		if (HuaXinApplication.isNetWorkConnected()) {
		String localPicPath = ImageUtils.savePicToCache(this, bitmap);
		Log.e("localPicPath", localPicPath);
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", rowsBean.getId() + ""));
		params.add(new BasicNameValuePair("fileType", "2"));
		params.add(new BasicNameValuePair("pictureFile", localPicPath));
		final String url = UrlUtils.picture_url;
		progress.show();
		new Thread() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
				String result = FileImageUpload.postFile(url, params);
				Log.e("chaijieupload_result", result);
				Message message = new Message();
				message.what = 3;
				message.obj = result;
				handler.sendMessage(message);
			}
		}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_che1:
			zhaopianid = 1;
			showPopupMenu(im_che1, false, "other");
			break;
		case R.id.im_che2:
			zhaopianid = 2;
			showPopupMenu(im_che2, false, "other");
			break;
		case R.id.im_che3:
			zhaopianid = 3;
			showPopupMenu(im_che3, false, "other");
			break;
		}
		return true;
	}

	protected void lookBigImage(String picUrl2) {
		// TODO Auto-generated method stub
		final ArrayList<String> urllist = new ArrayList<String>();
		urllist.add(picUrl2);
		new PhotosDialog(this, R.style.Dialog).show(0, urllist);
	}
}
